package com.akk.module.avs.service;

import com.akk.common.enums.Enums;
import com.akk.module.avs.entity.AvsPayOrder;
import com.akk.module.avs.entity.AvsPayOrderExample;
import com.akk.module.avs.mapper.AvsPayOrderMapper;
import com.akk.module.avs.mapper.dao.AvsPayOrderDao;
import com.akk.module.avs.model.response.AvsPayOrderRecordRes;
import com.akk.module.avs.model.response.AvsPayOrderSummaryRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AvsPayOrderService {

    @Autowired
    AvsPayOrderExample avsPayOrderExample;
    @Autowired
    AvsPayOrderMapper avsPayOrderMapper;

    @Autowired
    private AvsPayOrderDao payOrderDao;

    public List<AvsPayOrder> findByOrderNum(String orderNum) {
        avsPayOrderExample.clear();
        avsPayOrderExample.createCriteria().andOrderNumEqualTo(orderNum);
        return avsPayOrderMapper.selectByExample(avsPayOrderExample);
    }

    public AvsPayOrderSummaryRes countByStatus(Integer status){
        return payOrderDao.summary(status);
    }

    public AvsPayOrderRecordRes findByOrderId(String orderId){
        return payOrderDao.selectById(orderId);
    }

    public AvsPayOrderRecordRes queryInfoById(String orderId){
        return payOrderDao.queryInfo(orderId);
    }

    public void deleteByOrderId(String orderId){
        avsPayOrderMapper.deleteByPrimaryKey(orderId);
    }

    public void deleteByDate(Date date){
        avsPayOrderExample.clear();
        avsPayOrderExample.createCriteria().andCreateDateLessThan(date).andStatusEqualTo(Enums.OrderStatusEnum.NO_PAYED.getValue());
        avsPayOrderMapper.deleteByExample(avsPayOrderExample);
    }

    public List<AvsPayOrderRecordRes> findByPayerIdAndAppId(String payerId, String appId){
        Map params = new HashMap();
        params.put("appId",appId);
        params.put("payerId",payerId);
        return payOrderDao.selectByAppIdAndPayerId(params);
    }

    public void save(AvsPayOrder order) {
        avsPayOrderMapper.insert(order);
    }

    public void update(AvsPayOrder order) {
        avsPayOrderMapper.updateByPrimaryKey(order);
    }

    public Double payCount(String payerId){
        return payOrderDao.countPay(payerId);
    }

    public List<AvsPayOrderRecordRes> findByParams(Map params) {
        return payOrderDao.selectByParams(params);
    }
}
